// miniprogram/pages/home/bless/bless.js

const api = require("../../../utils/api");

var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    blessActivityId: "",
    playMusic: 0,
    imageUrl: app.globalData.imageUrl,
    imgList: [{
      id: 1,
      imgwidth: 0,
      imgheight: 0,
      name: "脫單",
      active: ""
    }, {
      id: 2,
      imgwidth: 0,
      imgheight: 0,
      name: "暴富",
      active: ""
    }, {
      id: 3,
      imgwidth: 0,
      imgheight: 0,
      name: "變美丽",
      active: ""
    }, {
      id: 4,
      imgwidth: 0,
      imgheight: 0,
      name: "苗條",
      active: ""
    }, {
      id: 5,
      imgwidth: 0,
      imgheight: 0,
      name: "健康長壽",
      active: ""
    }, {
      id: 6,
      imgwidth: 0,
      imgheight: 0,
      name: "順遂",
      active: ""
    }, {
      id: 7,
      imgwidth: 0,
      imgheight: 0,
      name: "万事順",
      active: ""
    }, {
      id: 8,
      imgwidth: 0,
      imgheight: 0,
      name: "好运来",
      active: ""
    }, {
      id: 9,
      imgwidth: 0,
      imgheight: 0,
      name: "幸福",
      active: ""
    }, {
      id: 10,
      imgwidth: 0,
      imgheight: 0,
      name: "吉祥如意",
      active: ""
    }, {
      id: 11,
      imgwidth: 0,
      imgheight: 0,
      name: "幸運",
      active: ""
    }, {
      id: 12,
      imgwidth: 0,
      imgheight: 0,
      name: "合家歡樂",
      active: ""
    }, {
      id: 13,
      imgwidth: 0,
      imgheight: 0,
      name: "步步高升",
      active: ""
    }, {
      id: 14,
      imgwidth: 0,
      imgheight: 0,
      name: "學業猛進",
      active: ""
    }, {
      id: 15,
      imgwidth: 0,
      imgheight: 0,
      name: "帥氣",
      active: ""
    }, {
      id: 16,
      imgwidth: 0,
      imgheight: 0,
      name: "博學",
      active: ""
    }, {
      id: 17,
      imgwidth: 0,
      imgheight: 0,
      name: "事業興",
      active: ""
    }, {
      id: 18,
      imgwidth: 0,
      imgheight: 0,
      name: "百事吉",
      active: ""
    }, {
      id: 19,
      imgwidth: 0,
      imgheight: 0,
      name: "財源廣進",
      active: ""
    }],
    words: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.initMod(this);
    this.data.blessActivityId = app.globalData.blessActivityId;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  imageLoad: function (i) {
    var t = this,
      e = i.currentTarget.dataset.id,
      a = i.detail.width,
      s = i.detail.height;
    t.data.imgList.forEach(function (i, t) {
      i.id == e && (i.imgwidth = a, i.imgheight = s);
    }), t.setData({
      imgList: t.data.imgList
    });
  },

  getContents: function (i) {
    var t = i.detail.value;
    this.data.words;
    this.setData({
      contents: t
    });
  },

  blur: function () {
    var i = this.data.contents,
      t = this.data.words;
    i && (t.push(i), this.setData({
      words: t
    }));
  },

  focus: function () {
    var i = this,
      e = this.data.contents,
      a = this.data.words;
    e ? (i.setData({
      isDisabled: !1
    }), a.forEach(function (i, t) {
      i == e && a.splice(t, 1);
    }), i.setData({
      words: a
    })) : a.length < 3 ? (i.setData({
      isDisabled: !1
    }), a.forEach(function (i, t) {
      i == e && a.splice(t, 1);
    }), i.setData({
      words: a
    })) : (i.setData({
      isDisabled: !0
    }), wx.showToast({
      title: '最多三个祈福语',
    }));
  },

  select: function (event) {
    var self = this;
    self.data.imgList.forEach(function (item, index) {
      if (item.id == event.currentTarget.dataset.id) {
        if (item.active == "") {
          if (self.data.words.length < 3) {
            self.data.imgList[index].active = "active";
            self.data.words.push(item.name);
          } else {
            wx.showToast({
              title: '最多三个祈福语',
            });
          }
        } else {
          self.data.imgList[index].active = "";
          self.data.words.forEach(function (wordsItem, wordsIndex) {
            if (wordsItem == item.name) {
              self.data.words.splice(wordsIndex, 1);
              self.setData({
                isDisabled: 0,
              });
            }
          });
        }
      }
    });
    self.setData({
      imgList: self.data.imgList,
      words: self.data.words,
    });
  },

  playMusic: function (e) {
    app.playMusic(this, e);
  },

  submit: function () {
    if (this.data.words.length < 1) {
      wx.showToast({
        title: '请选择祈福语',
      });
      return;
    }
    if (this.data.words.length > 3) {
      wx.showToast({
        title: '最多三个祈福语',
      });
      return;
    }

    api.postBlessRoundResult(app.globalData.blessActivityId, this.data.words).then(postResult=>{
      app.text('祈福成功');
      wx.redirectTo({
        url: '/pages/home/bell/index',
      });
    }).catch(res=>{
      app.text('祈福失败，请重试');
    });
  },
})